package fileIO;
/*
 * 未明白，P278
 */
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static java.lang.System.out;

public class ReverseStringCallable implements Callable<String> {
	private String in;
	public ReverseStringCallable(String in) {
		this.in = in;
	}
	public String call() throws Exception {
		Thread.sleep(3000);
		StringBuffer sb = new StringBuffer(in);
		return sb.reverse().toString();
	}
	
	public static void main(String[] args) {
		ExecutorService es = Executors.newSingleThreadExecutor();
		Callable<String> c = new ReverseStringCallable("hello");
		Future<String> f = es.submit(c);
		int sum=0;
		for (int i = 0; i < 100; i++) {
			sum+=i;
		}
		out.printf("sum=%d%n",sum);
		try {
			out.printf("wait for the result of the Future%n");
			out.printf("reverse('hello')=%s%n", f.get());
			es.shutdown();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
